package com.example.competitionregistration.service.impl;

import com.example.competitionregistration.common.Result;
import com.example.competitionregistration.common.ErrorCode;
import com.example.competitionregistration.controller.dto.UserDto;
import com.example.competitionregistration.entity.Admin;
import com.example.competitionregistration.entity.InstructorAccount;
import com.example.competitionregistration.entity.StudentAccount;
import com.example.competitionregistration.mapper.LoginMapper;
import com.example.competitionregistration.service.LoginService;
import com.example.competitionregistration.util.JwtUtil;
import com.example.competitionregistration.util.SHA1Util;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author QuanJingFang
 * @Description //TODO 登录业务层，进行数据校验与密码加密
 * @Date 2023/7/17 21:24
 **/
@Service
public class LoginServiceImpl implements LoginService {
    @Resource
    private LoginMapper loginMapper;


    /**
     * @Author QuanJingFang
     * @Description //TODO 对学生登录数据进行校验及生成jwt令牌
     * @Date 2023/7/18 17:04
     * @Param [studentDto]
     * @return com.example.competitionregistration.entity.Result
     **/
    @Override
    public Result login(UserDto userDto) {
        String checkOUt="\\w{5,}";
        if(userDto.getAccount().matches(checkOUt)==false||userDto.getPassword().matches(checkOUt)==false){
            return Result.error(ErrorCode.VERIFICATION_ERROR.getCode(), ErrorCode.VERIFICATION_ERROR.getMsg());
        }
        String password=SHA1Util.SHA1(userDto.getPassword());
        userDto.setPassword(password);
        StudentAccount studentAccount=loginMapper.studentLogin(userDto);
        if(studentAccount!=null){
            Map<String,Object> claims=new HashMap<>();
            claims.put("id",studentAccount.getId());
            claims.put("account",studentAccount.getAccount());
            String jwt= JwtUtil.generate(claims);
            userDto.setStatus(studentAccount.getStatus());
            userDto.setJwt(jwt);
            return Result.success(userDto);
        }
        InstructorAccount instructorAccount=loginMapper.teacherLogin(userDto);
        if(instructorAccount!=null){
            Map<String,Object> claims=new HashMap<>();
            claims.put("id",instructorAccount.getId());
            claims.put("account",instructorAccount.getAccount());
            String jwt= JwtUtil.generate(claims);
            userDto.setStatus(instructorAccount.getStatus());
            userDto.setJwt(jwt);
            return Result.success(userDto);
        }
        Admin admin=loginMapper.adminLogin(userDto);
        if(admin!=null){
            Map<String,Object> claims=new HashMap<>();
            claims.put("id",admin.getId());
            claims.put("account",admin.getAccount());
            String jwt= JwtUtil.generate(claims);
            userDto.setStatus("admin");
            userDto.setJwt(jwt);
            return Result.success(userDto);
        }
        return Result.error(ErrorCode.ACCOUNT_OR_PASSWORD_NO_EXIST_ERROR.getCode(), ErrorCode.ACCOUNT_OR_PASSWORD_NO_EXIST_ERROR.getMsg());
    }

}
